// script.js
let gameBoard = ['', '', '', '', '', '', '', '', ''];
let currentPlayer = 'X';
let gameActive = true;
let playerXIndices = [];
let playerOIndices = [];
let removeOldest = false; // 标记是否需要移除最早落下的棋子

const winningConditions = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6]
];

const cells = Array.from(document.getElementsByClassName('cell'));
const gameStatus = document.getElementById('game-status');
const resetButton = document.getElementById('reset-button');

cells.forEach(cell => {
    cell.addEventListener('click', cellClicked);
});

resetButton.addEventListener('click', resetGame);

function cellClicked(e) {
    const cellIndex = parseInt(e.target.getAttribute('data-index'));

    if (gameBoard[cellIndex] !== '' || !gameActive) {
        return;
    }

    updateGameBoard(cellIndex);
    checkGameStatus();
    switchPlayer();

    // 当一个玩家落下第四个棋子时，闪烁最早落下的棋子
    if ((currentPlayer === 'X' && playerXIndices.length === 3) ||
        (currentPlayer === 'O' && playerOIndices.length === 3)) {
        removeOldest = true; // 标记需要移除最早落下的棋子
        const oldestIndex = currentPlayer === 'X' ? playerXIndices[0] : playerOIndices[0];
        cells[oldestIndex].classList.add('blink');
    }
}

function updateGameBoard(index) {
    gameBoard[index] = currentPlayer;
    cells[index].textContent = currentPlayer;

    // 记录玩家的棋子位置
    if (currentPlayer === 'X') {
        playerXIndices.push(index);
    } else {
        playerOIndices.push(index);
    }

    // 如果需要移除最早落下的棋子，则移除
    if (removeOldest) {
        const oldestIndex = currentPlayer === 'X' ? playerXIndices.shift() : playerOIndices.shift();
        gameBoard[oldestIndex] = '';
        cells[oldestIndex].textContent = '';
        cells[oldestIndex].classList.remove('blink');
        removeOldest = false; // 重置移除标记
    }
}

function checkGameStatus() {
    let roundWon = false;
    for (let i = 0; i < winningConditions.length; i++) {
        const condition = winningConditions[i];
        const cellA = gameBoard[condition[0]];
        const cellB = gameBoard[condition[1]];
        const cellC = gameBoard[condition[2]];

        if (cellA === '' || cellB === '' || cellC === '') {
            continue;
        }
        if (cellA === cellB && cellB === cellC) {
            roundWon = true;
            break;
        }
    }

    if (roundWon) {
        gameStatus.textContent = `Player ${currentPlayer} has won!`;
        gameActive = false;
    } else if (!gameBoard.includes('')) {
        gameStatus.textContent = 'It\'s a draw!';
        gameActive = false;
    }
}

function switchPlayer() {
    currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
}

function resetGame() {
    gameBoard = ['', '', '', '', '', '', '', '', ''];
    currentPlayer = 'X';
    gameActive = true;
    gameStatus.textContent = '';
    cells.forEach(cell => {
        cell.textContent = '';
        cell.classList.remove('blink'); // 重置时移除所有闪烁效果
    });
    playerXIndices = [];
    playerOIndices = [];
    removeOldest = false; // 重置移除标记
}